package com.example.webbackend.controller;

import com.example.webbackend.model.User;
import com.example.webbackend.model.Group;
import com.example.webbackend.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {
    private final UserService userService;

    // 添加构造器注入
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody User user) {
        Map<String, Object> response = new HashMap<>();
        User registeredUser = userService.register(user);
        if (registeredUser != null) {
            response.put("success", true);
            response.put("message", "注册成功");
            response.put("userId", registeredUser.getId());
            return ResponseEntity.ok(response);
        } else {
            response.put("success", false);
            response.put("message", "用户名已存在");
            return ResponseEntity.badRequest().body(response);
        }
    }

    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestParam String username, 
                                                   @RequestParam String password) {
        Map<String, Object> response = new HashMap<>();
        User user = userService.login(username, password);
        if (user != null) {
            response.put("message", "登录成功");
            response.put("user", user);
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "用户名或密码错误");
            return ResponseEntity.status(401).body(response);
        }
    }

    @PostMapping("/logout")
    public String logout(@RequestParam String username) {
        userService.logout(username);
        return "退出成功";
    }

    @PostMapping("/updateProfile")
    public String updateProfile(@RequestParam String username, @RequestParam String profileInfo) {
        if (userService.updateProfile(username, profileInfo)) {
            return "个人资料更新成功";
        } else {
            return "用户不存在";
        }
    }
    
    // 新增兴趣标签管理
    @PutMapping("/{userId}/tags")
    public User updateUserTags(@PathVariable Long userId, @RequestBody List<String> tags) {
        return userService.updateUserTags(userId, tags);
    }
    
    @GetMapping("/{userId}/tags")
    public List<String> getUserTags(@PathVariable Long userId) {
        return userService.getUserTags(userId);
    }

    @PutMapping("/{userId}/status")
    public User updateUserStatus(@PathVariable Long userId, 
                               @RequestParam String status) {
        return userService.updateUserStatus(userId, status);
    }

    @GetMapping("/{userId}/profile")
    public User getUserProfile(@PathVariable Long userId) {
        return userService.getUserProfile(userId);
    }

    @GetMapping("/searchByTag")
    public List<User> searchUsersByTag(@RequestParam String tag) {
        return userService.searchUsersByTag(tag);
    }
}